﻿using System.Collections.Generic;
using System.Text;
using CommandLine;
using System.Diagnostics.Contracts;

namespace SetVersionNumber {

#pragma warning disable 0649

	// ReSharper disable UnassignedField.Global

	/// <summary>
	/// The program options
	/// </summary>
	internal class Options : CommandLineOptionsBase {

		/// <summary>
		/// Create a new options class.
		/// </summary>
		/// <param name="updater">The updater instance</param>
		public Options( FileVersionUpdater updater ) {
			Contract.Requires(updater!=null, "The updater must not be null");
			Contract.Ensures(_updater==updater, "The updater must be remembered");
			_updater = updater;
		}


		// ReSharper disable UnusedMember.Local
		[ContractInvariantMethod]
		private void ObjectInvariants( ) {
			Contract.Invariant(_updater!=null, "The file updater should not be null");
		}
		// ReSharper restore UnusedMember.Local


		/// <summary>
		/// The explicitly desired new version number.
		/// </summary>
		[Option("v", "version", Required=false, HelpText="Specify the exact version number to use.")]
		public string Version;


		/// <summary>
		/// The version strategy to use
		/// </summary>
		[Option("s", "strategy", Required=false, HelpText="Specify the version number incrementing strategy to use.")]
		public string StrategyName;


		/// <summary>
		/// The collection of files
		/// </summary>
		[ValueList(typeof(List<string>))]
		public IList<string> FileNames;


		/// <summary>
		/// Display help about the application
		/// </summary>
		/// <returns></returns>
		[HelpOption(HelpText="Display this help text.")]
		public string GetUsage( ) {
			Contract.Ensures(Contract.Result<System.String>()!=null);

			// Display the options
			var usage = new StringBuilder();
			usage.AppendLine("--version <version>     The version to use if using the 'explicit' versioning strategy.");
			usage.AppendLine("--file <file>           Specify the file to update.");

			// Include a collection of strategies
			usage.AppendLine("--strategy <strategy>   Defines the versioning strategy to use");
			usage.AppendLine("                        Available versioning strategies:");
			foreach( var strategy in _updater.VersionStrategies ) {
				const string template = "                        {0:10} - {1}";
				string strategyHelp = string.Format(template, strategy.Name, strategy.Description);
				usage.AppendLine(strategyHelp);
			}//foreach

			var result = usage.ToString();
			return result;
		}


		/// <summary>
		/// The file version updater instance, used to print out the list of strategies.
		/// </summary>
		private readonly FileVersionUpdater _updater;

	}//class

	// ReSharper restore UnassignedField.Global
#pragma warning restore 0649

}//namespace
